/*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2012 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

#include <links_widgets.h>

extern char p_ai[8][16];
extern char p_di[8][16];
extern char p_do[8][16];
extern char p_vt[16][16];

// {{{ cb_link_src_selector_activated()
G_MODULE_EXPORT void cb_link_src_selector_activated(GtkWidget *w, 
	link_selector_widgets_t *data)
{
	const char *option = gtk_menu_item_get_label(GTK_MENU_ITEM(w));
	strncpy(data->sp_selected, option, sizeof(data->sp_selected));
}
// }}}

// {{{ cb_link_dst_selector_activated()
G_MODULE_EXPORT void cb_link_dst_selector_activated(GtkWidget *w, 
	link_selector_widgets_t *data)
{
	const char *option = gtk_menu_item_get_label(GTK_MENU_ITEM(w));
	strncpy(data->dp_selected, option, sizeof(data->dp_selected));
}
// }}}

// {{{ cb_link_type_selector_activated()
G_MODULE_EXPORT void cb_link_type_selector_activated(GtkWidget *w, 
	link_selector_widgets_t *data)
{
	const char *option = gtk_menu_item_get_label(GTK_MENU_ITEM(w));
	strncpy(data->type_selected, option, sizeof(data->type_selected));
}
// }}}



// {{{ link_type_select()
void link_type_select(link_selector_widgets_t *lsw, const char* txt)
{
	gtk_widget_destroy(lsw->item1);
	gtk_widget_destroy(lsw->item2);
	gtk_widget_destroy(lsw->item3);
	gtk_widget_destroy(lsw->item4);
	gtk_widget_destroy(lsw->item5);
	gtk_widget_destroy(lsw->menu);

	lsw->menu = gtk_menu_new();

	lsw->item1 = gtk_menu_item_new_with_label ("--");
	lsw->item2 = gtk_menu_item_new_with_label ("Directo");
	lsw->item3 = gtk_menu_item_new_with_label ("Inverso");
	lsw->item4 = gtk_menu_item_new_with_label ("Pulso");
	lsw->item5 = gtk_menu_item_new_with_label ("Conmutado");

	gtk_signal_connect(GTK_OBJECT(lsw->item1), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);
	gtk_signal_connect(GTK_OBJECT(lsw->item2), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);
	gtk_signal_connect(GTK_OBJECT(lsw->item3), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);
	gtk_signal_connect(GTK_OBJECT(lsw->item4), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);
	gtk_signal_connect(GTK_OBJECT(lsw->item5), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);


	const char *item1 = 
			gtk_menu_item_get_label(GTK_MENU_ITEM(lsw->item1));
	const char *item2 = 
			gtk_menu_item_get_label(GTK_MENU_ITEM(lsw->item2));
	const char *item3 = 
			gtk_menu_item_get_label(GTK_MENU_ITEM(lsw->item3));
	const char *item4 = 
			gtk_menu_item_get_label(GTK_MENU_ITEM(lsw->item4));
	const char *item5 = 
			gtk_menu_item_get_label(GTK_MENU_ITEM(lsw->item5));


	if(strcmp(item1, txt)==0)
	{
		cb_link_type_selector_activated(lsw->item1, lsw);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item1);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item2);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item3);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item4);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item5);
	}
		
	else if(strcmp(item2, txt)==0)
	{
		cb_link_type_selector_activated(lsw->item2, lsw);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item2);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item3);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item4);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item5);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item1);
	}
		
	else if(strcmp(item3, txt)==0)
	{
		cb_link_type_selector_activated(lsw->item3, lsw);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item3);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item4);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item5);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item1);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item2);
	}
		
	else if(strcmp(item4, txt)==0)
	{
		cb_link_type_selector_activated(lsw->item4, lsw);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item4);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item5);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item1);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item2);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item3);
	}
		
	else if(strcmp(item5, txt)==0)
	{
		cb_link_type_selector_activated(lsw->item5, lsw);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item5);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item1);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item2);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item3);
		gtk_menu_append(GTK_MENU(lsw->menu),lsw->item4);
	}
	



	gtk_widget_show(lsw->item1);
	gtk_widget_show(lsw->item2);
	gtk_widget_show(lsw->item3);
	gtk_widget_show(lsw->item4);
	gtk_widget_show(lsw->item5);

	gtk_option_menu_set_menu(
		GTK_OPTION_MENU(lsw->opt), lsw->menu);
}
// }}}

// {{{ link_sport_select()
void link_sport_select(link_selector_widgets_t *lsw, const char* txt)
{
	int i;

	for(i=0; i<32; i++)
		gtk_widget_destroy(lsw->sp_item[i]);
	gtk_widget_destroy(lsw->sp_menu);

	lsw->sp_menu = gtk_menu_new();

	int j=0;
	for(i=0; i<8; i++)
		lsw->sp_item[j++] = gtk_menu_item_new_with_label (p_di[i]);

	for(i=0; i<8; i++)
		lsw->sp_item[j++] = gtk_menu_item_new_with_label (p_ai[i]);

	for(i=0; i<16; i++)
		lsw->sp_item[j++] = gtk_menu_item_new_with_label (p_vt[i]);

	for(i=0; i<32; i++)
	{
		gtk_signal_connect(GTK_OBJECT(lsw->sp_item[i]), "activate", 
			GTK_SIGNAL_FUNC(cb_link_src_selector_activated), lsw);

		const char *lbl =  
			gtk_menu_item_get_label(GTK_MENU_ITEM(lsw->sp_item[i]));

		if(strcmp(txt, lbl)==0)
		{
			gtk_menu_append(GTK_MENU(lsw->sp_menu),
				lsw->sp_item[i]);

			cb_link_src_selector_activated(lsw->sp_item[i], lsw);
		}
	}

	for(i=0; i<32; i++)
	{
		const char *lbl =  
			gtk_menu_item_get_label(GTK_MENU_ITEM(lsw->sp_item[i]));

		if(strcmp(txt, lbl)!=0)
			gtk_menu_append(GTK_MENU(lsw->sp_menu),
				lsw->sp_item[i]);

		gtk_widget_show(lsw->sp_item[i]);
	}

	gtk_option_menu_set_menu(
		GTK_OPTION_MENU(lsw->sp_opt), lsw->sp_menu);
}
// }}}

// {{{ link_dport_select()
void link_dport_select(link_selector_widgets_t *lsw, const char* txt)
{
	int i;

	for(i=0; i<24; i++)
		gtk_widget_destroy(lsw->dp_item[i]);
	gtk_widget_destroy(lsw->dp_menu);

	lsw->dp_menu = gtk_menu_new();

	int j=0;
	for(i=0; i<8; i++)
		lsw->dp_item[j++] = gtk_menu_item_new_with_label (p_do[i]);

	for(i=0; i<16; i++)
		lsw->dp_item[j++] = gtk_menu_item_new_with_label (p_vt[i]);

	for(i=0; i<24; i++)
	{
		gtk_signal_connect(GTK_OBJECT(lsw->dp_item[i]), "activate", 
			GTK_SIGNAL_FUNC(cb_link_dst_selector_activated), lsw);

		const char *lbl =  
			gtk_menu_item_get_label(GTK_MENU_ITEM(lsw->dp_item[i]));

		if(strcmp(txt, lbl)==0)
		{

			gtk_menu_append(GTK_MENU(lsw->dp_menu),
				lsw->dp_item[i]);

			cb_link_dst_selector_activated(lsw->dp_item[i], lsw);
		}
	}

	for(i=0; i<24; i++)
	{
		const char *lbl =  
			gtk_menu_item_get_label(GTK_MENU_ITEM(lsw->dp_item[i]));

		if(strcmp(txt, lbl)!=0)
			gtk_menu_append(GTK_MENU(lsw->dp_menu),
				lsw->dp_item[i]);

		gtk_widget_show(lsw->dp_item[i]);
	}

	gtk_option_menu_set_menu(
		GTK_OPTION_MENU(lsw->dp_opt), lsw->dp_menu);
}
// }}}

// {{{ link_widgets_create()
void link_widgets_create(
	link_selector_widgets_t *lsw, GtkWidget *parent, int x, int y)
{

   lsw->sp_opt = gtk_option_menu_new();
   lsw->sp_menu = gtk_menu_new();
	int i;
	for(i=0; i<8; i++)
	{
   	lsw->sp_item[i] = gtk_menu_item_new_with_label (p_di[i]);
		gtk_widget_show(lsw->sp_item[i]);
   	gtk_menu_append (GTK_MENU (lsw->sp_menu), lsw->sp_item[i]);
	
		gtk_signal_connect(GTK_OBJECT(lsw->sp_item[i]), "activate", 
			GTK_SIGNAL_FUNC(cb_link_src_selector_activated), lsw);
	}
		
	cb_link_src_selector_activated(lsw->sp_item[0], lsw);

	for(i=0; i<8; i++)
	{
   	lsw->sp_item[i+8] = gtk_menu_item_new_with_label (p_ai[i]);
		gtk_widget_show(lsw->sp_item[i+8]);
   	gtk_menu_append(GTK_MENU (lsw->sp_menu), lsw->sp_item[i+8]);
	
		gtk_signal_connect(GTK_OBJECT(lsw->sp_item[i+8]), "activate", 
			GTK_SIGNAL_FUNC(cb_link_src_selector_activated), lsw);
	}
	for(i=0; i<16; i++)
	{
   	lsw->sp_item[i+16] = gtk_menu_item_new_with_label (p_vt[i]);
		gtk_widget_show(lsw->sp_item[i+16]);
   	gtk_menu_append(GTK_MENU (lsw->sp_menu),lsw->sp_item[i+16]);
	
		gtk_signal_connect(GTK_OBJECT(lsw->sp_item[i+16]), "activate", 
			GTK_SIGNAL_FUNC(cb_link_src_selector_activated), lsw);
	}

   gtk_option_menu_set_menu 
		(GTK_OPTION_MENU (lsw->sp_opt), lsw->sp_menu);
	gtk_fixed_put (GTK_FIXED(parent), lsw->sp_opt, x, y);
	gtk_widget_show(lsw->sp_opt);


   lsw->dp_opt = gtk_option_menu_new();
   lsw->dp_menu = gtk_menu_new();
	for(i=0; i<8; i++)
	{
   	lsw->dp_item[i] = gtk_menu_item_new_with_label (p_do[i]);
		gtk_widget_show(lsw->dp_item[i]);
   	gtk_menu_append (GTK_MENU (lsw->dp_menu), lsw->dp_item[i]);

		gtk_signal_connect(GTK_OBJECT(lsw->dp_item[i]), "activate", 
			GTK_SIGNAL_FUNC(cb_link_dst_selector_activated), lsw);
	}

	cb_link_dst_selector_activated(lsw->dp_item[0], lsw);

	for(i=0; i<16; i++)
	{
   	lsw->dp_item[i+8] = gtk_menu_item_new_with_label (p_vt[i]);
		gtk_widget_show(lsw->dp_item[i+8]);
   	gtk_menu_append(GTK_MENU (lsw->dp_menu),lsw->dp_item[i+8]);

		gtk_signal_connect(GTK_OBJECT(lsw->dp_item[i+8]), "activate", 
			GTK_SIGNAL_FUNC(cb_link_dst_selector_activated), lsw);
	}

   gtk_option_menu_set_menu 
		(GTK_OPTION_MENU (lsw->dp_opt), lsw->dp_menu);
	gtk_fixed_put (GTK_FIXED(parent), lsw->dp_opt, x+80, y);
	gtk_widget_show(lsw->dp_opt);



   lsw->opt = gtk_option_menu_new();
   lsw->menu = gtk_menu_new();
   
	lsw->item1 = gtk_menu_item_new_with_label("--");
	lsw->item2 = gtk_menu_item_new_with_label("Directo");
   lsw->item3 = gtk_menu_item_new_with_label("Inverso");
   lsw->item4 = gtk_menu_item_new_with_label("Pulso");
   lsw->item5 = gtk_menu_item_new_with_label("Conmutado");

	gtk_widget_show(lsw->item1);
	gtk_widget_show(lsw->item2);
	gtk_widget_show(lsw->item3);
	gtk_widget_show(lsw->item4);
	gtk_widget_show(lsw->item5);

	cb_link_type_selector_activated(lsw->item1, lsw);

	gtk_signal_connect(GTK_OBJECT(lsw->item1), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);
	gtk_signal_connect(GTK_OBJECT(lsw->item2), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);
	gtk_signal_connect(GTK_OBJECT(lsw->item3), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);
	gtk_signal_connect(GTK_OBJECT(lsw->item4), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);
	gtk_signal_connect(GTK_OBJECT(lsw->item5), "activate", 
		GTK_SIGNAL_FUNC(cb_link_type_selector_activated), lsw);

   gtk_menu_append(GTK_MENU(lsw->menu), lsw->item1);
   gtk_menu_append(GTK_MENU(lsw->menu), lsw->item2);
   gtk_menu_append(GTK_MENU(lsw->menu), lsw->item3);
   gtk_menu_append(GTK_MENU(lsw->menu), lsw->item4);
   gtk_menu_append(GTK_MENU(lsw->menu), lsw->item5);

   gtk_option_menu_set_menu(GTK_OPTION_MENU(lsw->opt), lsw->menu);
	gtk_fixed_put(GTK_FIXED(parent), lsw->opt, x+160, y);
   gtk_widget_show(lsw->opt);
}
// }}}}


